/**
 * 2011-6-15 陈伟斌
 */
var PieChart
=function(){
	var piePlot;
	var optionPie;
	var attribute;
	var colors=['#FF0000','#0033FF','#FFFF00']
	var pieElementNode;
	/**
	 * 
	 * @param pieElement
	 *            要显示饼图的div的jquery对象
	 * @param dataMap
	 *            存储数据的map格式如{缴费率:0.6,未缴费率:0.3},值是小数，在前台会自动转成百分比
	 *  @param colorArray各块饼图的颜色如['#fff','#f00'] 为空则设为默认值
	 * @return
	 */
	 this.buildPieChart=function(pieElement,dataMap,colorArray){
		
		/** ***************设置 begin*************** */
		pieElementNode=pieElement;
		optionPie = {
				series: {
					pie: {
						show: true,
						radius: 0.6,﻿
						innerRadius:0,

						offset: {
							top: 0,
							left: 'auto'
						},
						stroke: {
							color: '#FFF',
							width: 1
					 },
				label: {
					show: true,
					formatter: function(label, slice){
				
					return '<div style="font-size:14;text-align:center;padding:2px;color:'+slice.color+';">'+label+'<br/>'+(Math.round(slice.percent*100,2)/100.0)+'%</div>';
				 },﻿
				radius: 1,﻿ 
				background: {
					color: null,
					opacity: 0
				},
					threshold: 0﻿ 
				},
				combine: {
					threshold: -1,﻿ // percentage at which to combine little
									// slices into one larger slice
					color: null,﻿ // color to give the new slice
									// (auto-generated if null)
					label: 'Other'﻿ // label to give the new slice
				},
				highlight: {
					// color: '#FFF',﻿ ﻿ // will add this functionality once
					// parseColor is available
					opacity: 0.5
				}
					}
					}
				};
		if(colorArray!=null)
			colors=colorArray;
		/** ***************设置 end*************** */
		

		dataMap=dealPieData(dataMap);
		piePlot= $.plot(pieElement ,dataMap, optionPie);

		
		
	}
  
//  
//
//	function updatePie(pieElement,topic,param,attributeName) {
//		
//		if(param==null)
//			param={};
//		param.date=new Date();
//		$.post(topic,param,function(data){
//			if(typeof data !='object'){
//				data=eval("("+data+")")
//			}
//			dataMap=eval("("+data[attributeName]+")");
//			dataMap=dealPieData(dataMap);
//			piePlot= $.plot(pieElement ,dataMap, optionPie);
//		})
//        setTimeout(function(){updatePie(pieElement,topic,param,attributeName)}, 5000);
//		
//    }
	function dealPieData(dataMap)
	{

		var pieData = [];
		var i=0;
		for(var item in dataMap){
			//alert(dataMap[item]* 1000 / 10)
			pieData.push( { label: item, data: [ [1, dataMap[item]]] ,color:colors[i]});
			i++;
		}
		return pieData;
	}
	this.update=function(data){
			data = decodeURIComponent(data);
//			alert(data)
			if(typeof data !='object'){
				data=eval("("+data+")")
			}
			dataMap=dealPieData(data);
			piePlot= $.plot(pieElementNode ,dataMap, optionPie);
	}

}
